package com.example.mynetwork2.login2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mynetwork2.MainActivity;
import com.example.mynetwork2.R;
import com.example.mynetwork2.login.LoginAct;
import com.example.mynetwork2.login.utils.Utils;
import com.example.mynetwork2.recyclerview.netrequest.ResponseBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginAct2 extends AppCompatActivity {
    private EditText input_username;
    private EditText input_password;
    private Button login_bt;
    private TextView register_tv;
    private LinearLayout progress_bar;
    private OkHttpClient okHttpClient = new OkHttpClient();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        input_username = (EditText) findViewById(R.id.et_loginactivity_username);
        input_password = (EditText) findViewById(R.id.et_loginactivity_password);
        login_bt = findViewById(R.id.bt_loginactivity_login);
        register_tv=findViewById(R.id.tv_loginactivity_register);
        progress_bar = findViewById(R.id.progress_bar);
        login_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        register_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//注册

            }
        });


    }

    private void login() {
        String username = input_username.getText().toString();
        if (Utils.isEmpty(username)){
            Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        String password = input_password.getText().toString();
        if (Utils.isEmpty(password)){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        //显示进度
        progress_bar.setVisibility(View.VISIBLE);
        UserBean userBean = new UserBean();
        if (Utils.checkEmail(username)){//用户输入的是email
            userBean.setEmail(username);
        }else if (Utils.isMobile(username)){//用户输入的是手机号
            userBean.setPhone(username);
        }else {
            userBean.setUsername(username);
        }
        userBean.setPassword(password);
        Gson gson = new Gson();
        String userInfoJson = gson.toJson(userBean);
        String url = "http://10.111.34.9:8080/user/login";
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(userInfoJson, mediaType))
                .build();
        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                LoginAct2.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progress_bar.setVisibility(View.GONE);
                        Toast.makeText(LoginAct2.this,e.getMessage(),Toast.LENGTH_SHORT);
                    }
                });

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String s  = response.body().string();
                Gson gson1 = new Gson();
//                Map<String ,Object> map = gson1.fromJson(s, Map.class);
                ResponseEntity responseEntity = gson1.fromJson(s, ResponseEntity.class);
                Integer code1 = responseEntity.getCode();
                //Double code = (Double) map.get("code");
                if (code1!=null&&code1==0){

                    LoginAct2.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progress_bar.setVisibility(View.GONE);
                        }
                    });

                    Intent intent = new Intent(LoginAct2.this, MainActivity.class);
                    LoginAct2.this.startActivity(intent);
                    //关闭当前的Activity
                    LoginAct2.this.finish();
                }else {
                    LoginAct2.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progress_bar.setVisibility(View.GONE);
                           Toast.makeText(LoginAct2.this, responseEntity.getMsg(),Toast.LENGTH_SHORT);
                        }
                    });
                }


            }
        });


    }
}
